/*
 * @Autor: Shihh
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-07 15:30:55
 */
const Sequelize = require('sequelize')

module.exports = (sequelize, DataTypes) => {

  class SearchBar extends Sequelize.Model {
    /**
     * @author: Shihh
     * @description: 分页获取
     * @param {type} 
     * @return: 
     */
    static async getSearchBar(pars) {
      var [page = 1, pagesize = 15, limit = 0,status=null] = [pars.page, pars.pagesize, pars.limit,pars.status]
      let condition = {
        attributes: ['id', 'name', 'status', 'sort', 'position', 'link_type', 'link_id', 'link','link_name'],
        raw: true,
        offset: pagesize * (page - 1),
        limit: parseInt(pagesize),
        order: [['sort', 'ASC']]
      }
      if(status!==null&&status!==undefined){
        let where={}
        where.status=status
        condition.where=where
      }
      let list = await this.findAll(condition)
      return list;
    }

    /**
     * @author: Shihh
     * @description: 修改
     * @param {type} 
     * @return: 
     */
    static async modifySearchStatus(pars) {
      var status=pars.status
      var id=pars.id
      let result=await this.update({status},{where:{id}})
      return result;
    }

    /**
     * @author: Shihh
     * @description: 新增搜索Bar
     * @param {type} 
     * @return: 
     */
    static async addSearchBar(form) {
      return await this.create({
        name: form.name,
        status: form.status,
        sort: form.sort,
        link: form.link,
        link_id: form.link_id == '' ? null : form.link_id,
        link_type: form.link_type,
        position: form.position,
        link_name:form.link_name
      }).then(re => {
        return true
      }).catch(err => {
        console.log(err)
        return false
      })
    }

    /**
     * @author: Shihh
     * @description: 删除搜索bar
     * @param {type} 
     * @return: 
     */
    static async deleteSearchBar(pars) {
      return await this.destroy({
        where: {
          id: pars.id,
        },
      }).then(re => {
        return true
      }).catch(err => {
        return false
      })
    }

    /**
     * @author: Shihh
     * @description: 编辑专题
     * @param {type} 
     * @return: 
     */
    static async editSearchBar(form) {

      return await this.update({
        name: form.name,
        status: form.status,
        sort: form.sort,
        link: form.link,
        link_id: form.link_id == '' ? null : form.link_id,
        link_type: form.link_type,
        position: form.position,
        link_name:form.link_name
      }, {
        'where': {
          'id': form.id
        }
      }).then(re => {
        return true
      }).catch(err => {
        return false
      })
    }

    /**
    * @author: Shihh
    * @description: 获取列表条数，用于排序
    * @param {type} 
    * @return: 
    */
    static async getSearchCount() {
      return await this.count();
    }


    /**
    * @author: Shihh
    * @description: 拖动排序
    * @param {type} 
    * @return: 
    */
    static async searchSort(params) {
      var queryData = []
      var valueArr = []
      // let sql = `select id,sort from search_bar order by sort asc`
      // queryData = await sequelize.query(sql)
      queryData=await this.findAll({attributes:['id','sort'],order:[['sort', 'ASC']]})
      //start拖动的目的地
      //end从哪一行开始拖动的
      //start《end从后往前拖动   start》end从前往后拖动
      var value = {}
      if (parseInt(params.start) < parseInt(params.end)) { //从后往前拖动
        for (var i = parseInt(params.start); i <= parseInt(params.end); i++) {
          console.log(i == parseInt(params.end))
          if (i == parseInt(params.end)) {
            value = {
              id: queryData[i].id,
              sort: parseInt(params.start) + 1
            };
          } else {
            value = {
              id: queryData[i].id,
              sort: queryData[i].sort + 1,
            };
          }

          valueArr.push(value)
        }
      } else if (parseInt(params.start) > parseInt(params.end)) { //从前往后拖动
        for (var j = parseInt(params.end); j <= parseInt(params.start); j++) {
          if (j == parseInt(params.end)) {
            value = {
              id: queryData[j].id,
              sort: parseInt(params.start) + 1
            }
          } else {
            value = {
              id: queryData[j].id,
              sort: queryData[j].sort - 1,
            };
          }

          valueArr.push(value)
        }
      }
      var updateData = []
      updateData = await this.bulkCreate(valueArr, {
        updateOnDuplicate: ["sort"]
      });
      return updateData
    }


  }

  SearchBar.init({
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    name: { type: DataTypes.STRING(45) },
    sort: { type: DataTypes.INTEGER },
    status: { type: DataTypes.INTEGER, defaultValue: 0 },
    position: { type: DataTypes.INTEGER(6) },
    link: { type: DataTypes.STRING(255) },
    link_id: { type: DataTypes.INTEGER },
    link_type: { type: DataTypes.STRING(255) },
    link_name:{ type: DataTypes.STRING(45) }
  }, {
    sequelize,
    modelName: 'search_bar',
    freezeTableName: true,
    timestamps: false
  });

  return SearchBar;
}